package api

import (
	"github.com/gin-gonic/gin"
	"server/internal/app/model"
	"server/internal/app/serv"
	"server/internal/database"
	"server/internal/pkg/response"
	"strconv"
)

// TissueApi 组织管理
var TissueApi = new(tissueApi)

type tissueApi struct {
	model.Tissue
}

// List 列表
func (a tissueApi) List(c *gin.Context) {
	pid, _ := strconv.Atoi(c.Query("pid"))
	rules, _ := c.Get("rules")
	rulesString := rules.(string)

	userInfo, _ := c.Get("userInfo")

	if pid == 0 {
		if rulesString == "*" {
			response.SuccessList(c, serv.Tissue.List(pid), 0)
			return
		}
		pid = userInfo.(model.User).TissueId
		response.SuccessList(c, serv.Tissue.Lists(pid), 0)
	} else {
		response.Success(c, serv.Tissue.List(pid), "")
	}

}

// Detail 单条
func (a tissueApi) Detail(c *gin.Context) {
	id := c.Query("id")

	_, detail := serv.Tissue.Read(id)

	response.SuccessData(c, detail, "")
}

// Add 添加
func (a tissueApi) Add(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		response.Fail(c, err.Error(), "参数错误")
		return
	}

	if a.Pid == 0 {
		response.Fail(c, "", "无法添加顶级组织")
		return
	}

	if err := serv.Tissue.Create(a.Tissue); err.Error == nil {
		response.Success(c, "", "添加成功")
	} else {
		response.Fail(c, err, "添加失败")
	}
}

// Edit 编辑
func (a tissueApi) Edit(c *gin.Context) {

	if err := c.ShouldBindJSON(&a); err != nil {
		response.Fail(c, err.Error(), "参数错误")
		return
	}

	if serv.Tissue.Update(a.Tissue).Error == nil {
		response.Success(c, "", "编辑成功")
	} else {
		response.Fail(c, "", "编辑失败")
	}
}

// Del 删除
func (a tissueApi) Del(c *gin.Context) {
	type Param struct {
		Ids []int `json:"ids"`
	}
	var param Param
	if err := c.ShouldBindJSON(&param); err != nil {
		response.Fail(c, err.Error(), "参数错误")
		return
	}
	if err := serv.Tissue.Delete(param.Ids); err != nil {
		response.Fail(c, "", err.Error())
	} else {
		response.Success(c, "", "删除成功")

	}
}

// GetSelect 远程下啦
func (a tissueApi) GetSelect(c *gin.Context) {
	pid, _ := strconv.Atoi(c.Query("pid"))
	rules, _ := c.Get("rules")
	rulesString := rules.(string)

	userInfo, _ := c.Get("userInfo")

	//if rulesString == "*" {
	//	response.Success(c, serv.Tissue.Select(0), "")
	//} else {
	//
	//	response.Success(c, serv.Tissue.Selects(userInfo.(model.User).TissueId), "")
	//}

	if pid == 0 {
		if rulesString == "*" {
			response.Success(c, serv.Tissue.Select(pid), "")
			return
		}
		pid = userInfo.(model.User).TissueId
		response.Success(c, serv.Tissue.Selects(pid), "")
	} else {
		response.Success(c, serv.Tissue.Select(pid), "")
	}

}

// StatList 带统计的列表
func (a tissueApi) StatList(c *gin.Context) {
	Db := database.DB         //初始化Db
	Db = Common.Paging(Db, c) //分页
	pid := c.Query("pid")
	rules, _ := c.Get("rules")
	rulesString := rules.(string)

	userInfo, _ := c.Get("userInfo")

	if pid == "" {
		if rulesString == "*" {
			Db = Db.Where("pid = ?", 0)
		} else {
			Db = Db.Where("id = ?", userInfo.(model.User).TissueId)
		}

	} else {
		Db = Db.Where("pid = ?", pid)
	}

	_, list, total := serv.Tissue.StatList(Db)

	response.SuccessList(c, list, total)
}

// GetIds 返回当前组织结构id数组
func (a tissueApi) GetIds(c *gin.Context) {

	id, _ := strconv.Atoi(c.Query("tissue_id"))

	response.Success(c, serv.Tissue.GetIds(id), "")
}
